//
//  AppDelegate.m
//  B段项目
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "AppDelegate.h"
#import "TOWelcomeViewController.h"
#import "HLJSeletionViewController.h"
#import "ZZCReadViewController.h"
#import "TODrawerViewController.h"
#import "APService.h"
#import "UMSocialWechatHandler.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeRootVC" object:nil];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     [UIApplication sharedApplication].applicationIconBadgeNumber = NO;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //首页
    HLJSeletionViewController *homeVC = [[HLJSeletionViewController alloc] init];
    self.homeNC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    self.homeNC.navigationBar.translucent = NO;


    //抽屉
    TODrawerViewController *drawer = [[TODrawerViewController alloc] init];
    
    self.leftSliderVC = [[LeftSlideViewController alloc] initWithLeftView:drawer andMainView:self.homeNC];
    

    //欢迎页
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"]) {
        
        //如果是第一次登录, 进入欢迎界面, 并将NSUserDefaults登陆设为1
        TOWelcomeViewController *welcomeVC = [[TOWelcomeViewController alloc] init];
        self.window.rootViewController = welcomeVC;
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirst"];
        
    }
    else
    {
        
        self.window.rootViewController = self.leftSliderVC;
        
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRootVC) name:@"changeRootVC" object:nil];
    
    [[ZZCCollectionData collectDataBase] openDB];
    [[ZZCCollectionData collectDataBase] createCollectListTable];
    [UMSocialData setAppKey:@"568342bc67e58e851f0019a2"];
    
    [UMSocialWechatHandler setWXAppId:@"wx6375c48466a329e2" appSecret:@"2507c5089ebf6a7a4e4628ec549412d9" url:@"http://moments.me/mobile/"];
    
    //1.第一步: 注册远程通知
    
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        
        
        //注册远程通知s
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
    
    // Required
    [APService setupWithOption:launchOptions];
    
    
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
    }
    return result;
}


//第一次运行欢迎页之后重设根视图
-(void)changeRootVC
{
    self.window.rootViewController = self.leftSliderVC;
}


//第二步 : 注册远程通知成功返回device token(内部实现)
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken:  %@",deviceToken);
    // Required
    //第三步: 将返回的devicetoken传给服务器(通常指所用的三方平台)
    [APService registerDeviceToken:deviceToken];
}

//第五步 : 收到远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    //返回后台 收到消息
  //  NSLog(@"#$%^R&Y*U*(");
    
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
   // NSLog(@"DFGHJKL");
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}








- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     [UIApplication sharedApplication].applicationIconBadgeNumber = NO;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
